<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014-2018 东莞市云拓互联网络科技有限公司
// +----------------------------------------------------------------------
// | 官方网站:http://www.ytclouds.net
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------

namespace app\lottery\controller;

use controller\BasicAdmin;
use service\DataService;
use service\LogService;
use think\Db;
use service\WechatService;
use think\Exception;
use \think\File;
use service\ToolsService;

/**
 * 应用入口控制器
 * @author Anyon <zoujingli@qq.com>
 */
class Winner extends BasicAdmin
{

    /**
     * 定义当前操作表名
     * @var string
     */
    public $table = 'LotteryWinner';

    /**
     * 中奖人列表
     * @return mixed
     */
    public function index()
    {
        $this->title = '中奖列表';
        $get = $this->request->get();
        $db = Db::name($this->table);
        if (isset($get['type']) && $get['type'] !== '') {
            $search_type = ($get['type'] == 1) ? 1 : 0;
            $db->where('type', $search_type);
        }

        if (isset($get['tag']) && $get['tag'] !== '') {
            $db->where('lottery_pool_id', $get['tag']);
        }

        if (isset($get['addtime']) && $get['addtime'] !== '') {
            list($start, $end) = explode(' - ', $get['addtime']);
            $start = strtotime($start);
            $end = strtotime($end);
            $db->whereBetween('addtime', ["{$start}", "{$end}"]);
        }

        $db->where("good_name","<>","谢谢惠顾");
        if (isset($get['is_report']) && $get['is_report'] == 1) {
            return parent::_list($db->order('addtime desc'), false);
        } else {
            //搜出全部
            return parent::_list($db->order('addtime desc'));
        }
    }

    /**
     * 列表数据处理
     * @param $list
     */
    protected function _data_filter(&$list)
    {
        foreach ($list as &$vo) {
            $user_info = Db::table("app_users")->where(['id' => $vo['user_id']])->field("id,nickname")->find();
            $vo['nickname'] = $user_info['nickname'];
            $pool_info = Db::table("lottery_pool")->where(['id' => $vo['lottery_pool_id']])->find();
            list($vo['original_img'], $vo['goods_name'], $vo['copywriter2'], $vo['href'], $vo['nickname']) =
                [$pool_info['original_img'], $pool_info['goods_name'], $pool_info['copywriter2'], $pool_info['href'], ToolsService::emojiDecode($vo['nickname'])];
        }
        if ($this->request->get('is_report') == 1) {
            if (count($list) <= 0) {
                $this->error("无数据");
            }
            //打印
            $strTable = '<table width="500" border="1">';
            $strTable .= '<tr><td colspan="7" style="text-align:center;font-size:18px;height: 32px;line-height: 32px;">中奖列表导出</td></tr>';
            $strTable .= '<tr>';
            $strTable .= '<td style="text-align:center;font-size:12px;" width="*" >奖品</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;" width="*">抽奖类型</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;" width="*">中奖人</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;" width="*">中奖CDkey</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;" width="*">中奖提示信息</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;" width="*">中奖跳转的链接</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;" width="*">中奖时间</td>';
            $strTable .= '</tr>';

            foreach ($list as $k => $v) {
                if ($v['type'] == 1) {
                    $type_desc = '高级抽奖';
                } else if ($v['type'] == 0) {
                    $type_desc = '普通抽奖';
                } else {
                    $type_desc = '未知';
                }
                $strTable .= '<tr>';
                $strTable .= '<td style="text-align:center;font-size:12px;" width="250" >' . $v['goods_name'] . '</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;" width="250" >' . $type_desc . '</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;" width="250" >' . $v['nickname'] . '</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;" width="250" >' . $v['cdkey'] . '</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;" width="250" >' . $v['copywriter2'] . '</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;" width="250" >' . $v['href'] . '</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;" width="250" >' . date("Y-m-d H:i:s", $v['addtime']) . '</td>';
                $strTable .= '</tr>';
            }

            $strTable .= '</table>';

            $filename = 'winner_';
            downloadExcel($strTable, $filename);
            exit;
        }
        $tags = Db::name($this->table)->group("lottery_pool_id")->column('id,good_name,lottery_pool_id');
        $this->assign('tags', $tags);
    }


}
